package io.netty.test.main.heartbeat;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.concurrent.EventExecutorGroup;

/**
 * Created by LuoYu on 2019/2/9.
 */
public class HeartbeatHandler extends ChannelInboundHandlerAdapter {
    /**
     * 用户事件触发
     * 事件来自pipeline前方注册的handler
     * 本例中是IdleStateHandler 检测连接时候为空闲状态 符合条件则会触发事件
     */
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

        if (evt instanceof IdleStateEvent) {
            // 空闲事件
            IdleStateEvent event = (IdleStateEvent) evt;
            String eventType = null;
            switch (event.state()) {
                case READER_IDLE:
                    eventType = "读空闲";
                    break;
                case WRITER_IDLE:
                    eventType = "写空闲";
                    break;
                case ALL_IDLE:
                    eventType = "读写空闲";
                    break;
            }
        }
    }
}
